package com.yoomiito.app.adapter.home.reommend;

import android.content.Context;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.yoomiito.app.R;
import com.yoomiito.app.model.ViewType;
import com.yoomiito.app.model.bean.HomeChosenAct;
import com.yoomiito.app.model.bean.HomeChosenGoods;
import com.yoomiito.app.utils.DensityUtil;
import com.yoomiito.app.utils.ImageLoader;
import com.yoomiito.app.widget.SpacingDecoration;

import java.util.ArrayList;
import java.util.List;

public class HomeChosenGoodsAdapter extends DelegateAdapter.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private LayoutHelper mLayoutHelper;
    private RecyclerView.RecycledViewPool viewPool;
    private List<HomeChosenAct> datas;
    private OnItemClickListener mOnItemClickListener;
    public HomeChosenGoodsAdapter(Context context, LayoutHelper layoutHelper, List<HomeChosenAct> datas,
                                  RecyclerView.RecycledViewPool viewPool, OnItemClickListener mOnItemClickListener) {
        mContext = context;
        mLayoutHelper = layoutHelper;
        this.viewPool = viewPool;
        this.mOnItemClickListener = mOnItemClickListener;
        this.datas = datas;
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mLayoutHelper;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_chosen_goods, viewGroup, false);
        return new ItemViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        HomeChosenGoodsAdapter.ItemViewHolder holder = (HomeChosenGoodsAdapter.ItemViewHolder) viewHolder;
        RecyclerView.LayoutParams lp= (RecyclerView.LayoutParams) holder.itemView.getLayoutParams();
        lp.topMargin = DensityUtil.dp2px(i==0?10:0);
        holder.titleIv.setVisibility(i == 0 ? View.VISIBLE:View.GONE);
        HomeChosenAct act = datas.get(i);
        ImageLoader.getInstance().into(mContext, act.getTopic_banner(), holder.iv,R.drawable.default_chosen_act);
        if (i!=0){
//            holder.itemView.setPadding(0,DensityUtil.dp2px(10),0,0);
        }
        holder.iv.setOnClickListener(v ->{
                    if (mOnItemClickListener!=null) mOnItemClickListener.onClick(i,-1,true);
        });
        if (act.getList() != null && act.getList().size() > 0) {
            holder.rcy.setRecycledViewPool(viewPool);
            LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
            layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
            holder.rcy.setLayoutManager(layoutManager);
            if (holder.rcy.getItemDecorationCount()==0){
//                holder.rcy.removeItemDecorationAt(0);
                holder.rcy.addItemDecoration(new SpacingDecoration(DensityUtil.dp2px(4),0,false));
            }
            List<ViewType<HomeChosenGoods>> goodsList = new ArrayList<>();
            for (HomeChosenGoods goods : act.getList()) {
                ViewType<HomeChosenGoods> type = new ViewType<>();
                type.setViewType(21);
                type.setT(goods);
                goodsList.add(type);
            }
            ViewType<HomeChosenGoods> type = new ViewType<>();
            type.setViewType(20);
            goodsList.add(type);
            HomeItemChosenGoodsAdapter itemAdapter = new HomeItemChosenGoodsAdapter(mContext, goodsList);
            holder.rcy.setAdapter(itemAdapter);
            itemAdapter.setOnItemClickListener((position, isLookMore) -> {
                if (mOnItemClickListener!=null) mOnItemClickListener.onClick(i,position,isLookMore);
            });
        }
    }

    @Override
    public int getItemCount() {
        return null == datas ? 0 : datas.size();
    }

    @Override
    public int getItemViewType(int position) {
        return 12;
    }

    public interface OnItemClickListener{
        void onClick(int parentPosition,int childPosition,boolean requestMore);
    }

    private class ItemViewHolder extends RecyclerView.ViewHolder {
        private RecyclerView rcy;
        private ImageView iv;
        private ImageView titleIv;

        public ItemViewHolder(@NonNull View itemView) {
            super(itemView);
            iv = itemView.findViewById(R.id.actBgIv);
            rcy = itemView.findViewById(R.id.rcy);
            titleIv = itemView.findViewById(R.id.titleIv);
        }
    }
}
